package com.lipei.vrp.condor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import com.lipei.vrp.Vrprun;

public class Main {

	public static void main(String[] args) {
		if (args == null || args.length == 0) {
			return;
		}

		File batchfile = new File(args[0]);
		try {
			batchFile(batchfile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void batchFile(File batchfile) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(batchfile)));
		ArrayList<String> lines = new ArrayList<String>();
		while (true) {
			String line = reader.readLine();
			if (line == null) {
				break;
			}
			if (StringUtils.isEmpty(line)) {
				continue;
			}
			lines.add(line);
		}

		System.out.println("Total line:" + lines.size());
		System.out.println("start queue:...");

		for (String string : lines) {
			runCondor(string);
		}

		System.out.println("queue done! good luck");
	}

	private static void runCondor(String string) {

		/**
		 * 
		 * */
		String[] args = string.split(" ");
		String dbkey = args[0];
		String algoithm = args[1];

		String vrpname = string.replace(" ", "_") + ".vrp";

		try {
			String path = Vrprun.runmysql(vrpname, dbkey);

			FileUtils.copyFile(new File(path), new File(vrpname));

			FileUtils.deleteQuietly(new File(path));

			/**
			 * gen condor file
			 * */

			StringBuffer buf = new StringBuffer();
			buf.append("universe = java\r\n");
			buf.append("executable = vrprun.jar\r\n");
			buf.append("jar_files = vrprun.jar\r\n");
			buf.append("arguments=");

			// argument
			buf.append(Subjob.class.getName());
			buf.append(" ");

			buf.append(vrpname);
			buf.append(" ");

			buf.append(algoithm);
			buf.append(" ");
			for (int i = 2; i < args.length; i++) {
				buf.append(args[i]);
				buf.append(" ");
			}

			buf.append("\r\n");

			buf.append("output=" + vrpname + ".output\r\n");
			buf.append("error=" + vrpname + ".error\r\n");
			buf.append("should_transfer_files = YES\r\n");
			buf.append("when_to_transfer_output = ON_EXIT\r\n");
			buf.append("transfer_input_files=vrp.exe,vrp_ej.exe,vrp_init.exe,vrp_rtr.exe,vrp_sa.exe,"
					+ vrpname + "\r\n");
			buf.append("transfer_output_files=" + vrpname + ".sql\r\n");
			buf.append("queue\r\n");
			File file = new File(vrpname + ".condor");
			FileUtils.write(file, buf, false);

			List<String> list = new ArrayList<String>();
			list.add("condor_submit");
			list.add(file.getName());
			ProcessBuilder builder = new ProcessBuilder(list);

			Process pro = builder.start();
			// pro.waitFor();
		} catch (IOException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
